<!--vue模板-->
<template>
  <el-dialog
    @close="close"
    v-model="visible"
    :close-on-click-modal="false"
    :title="ruleForm.id ? '更新分类' : '添加分类'"
    width="60%"
    draggable
  >
    <!-- 表单数据 -->
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="ruleForm"
      status-icon
    >
      <el-form-item label="分类名称" prop="name">
        <el-input
          v-model="ruleForm.name"
          class="ipt"
          placeholder="请输入分类名称"
        />
      </el-form-item>

      <el-form-item label="分类排序" prop="sorts">
        <el-input-number
          v-model="ruleForm.sorts"
          class="ipt"
          placeholder="请输入分类排序"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="saveCategory"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<!--vue 组件选项定义-->
<script setup>
import { inject, reactive, ref } from "vue";

import { ElMessage,ElMessageBox } from "element-plus";

import { saveCategory as saveCategoryInfo,updateCategory } from '@/api/basic/Category.js'

const ruleFormRef = ref(null);

const visible = inject("isShow");
const pidAdd = inject("pidAdd");

// 表单
const ruleForm = ref({
  id: "",
  name: "",
  sorts: "",
  pid: "",
});

function init(data){
  ruleForm.value = data
}

defineExpose({ init })

// 校验
const rules = {
  name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
};
const emit = defineEmits(["close"]);

function close() {
  delete ruleForm.value.id;
  visible.value = false;
  // 重置表单 去除之前的校验
  ruleFormRef.value.resetFields();
  emit("close");
}

// 暴露接口

// 保存用户信息
const saveCategory = () => {
  ruleFormRef.value.validate((val) => {
    console.log(val, "val");
    if (val) {
      // 更新用户
      if (ruleForm.value.id) {
        console.log("更新分类");
        console.log(ruleForm.value);
        updateCategory(ruleForm.value).then(resp => {
          ElMessage.success("更新分类成功！")
                close()
        })
      } else {
        // 添加用户
        ruleForm.value.pid = pidAdd.value;
        // 添加一级分类
        if (!pidAdd.value) {    
          ElMessageBox.confirm(
            "您未选择要添加的分类，当前默认选择的分类为一级分类，是否继续?",
            "警告",
            {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }
          )
            .then(() => {
              console.log(ruleForm.value, "value");
              saveCategoryInfo(ruleForm.value).then(resp => {
                ElMessage.success("添加分类成功！")
                close()
              })
            })
            .catch(() => {
              return;
            });
        }else {
          saveCategoryInfo(ruleForm.value).then(resp => {
                ElMessage.success("添加分类成功！")
                close()
              })
        }


        
      }
    }
  });
};
</script>

<!--vue样式 scoped当前样式只适用于当前组件-->
<style scoped>
.ipt {
  width: 90%;
}

.demo-date-picker {
  display: flex;
  width: 100%;
  padding: 0;
  flex-wrap: wrap;
}

.demo-date-picker .block {
  padding: 30px 0;
  text-align: center;
  border-right: solid 1px var(--el-border-color);
  flex: 1;
}

.demo-date-picker .block:last-child {
  border-right: none;
}

.demo-date-picker .demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

:deep(.avatar-uploader .el-upload) {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
